//
//  LSNewsTableViewSingleImageCell.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/26.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSNewsTableViewSingleImageCell.h"
#import "LSNewsViewModel.h"

@interface LSNewsTableViewSingleImageCell ()
@property (nonatomic, strong) UILabel *mainTitleLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIImageView *mainImageView;
@end

@implementation LSNewsTableViewSingleImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.mainTitleLabel];
        [self.contentView addSubview:self.addressLabel];
        [self.contentView addSubview:self.timeLabel];
    }
    return self;
}

- (void)setModel:(LSNewsViewModel *)model {
    _model = model;
    self.mainTitleLabel.text = model.title;
//    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.typeImg] placeholderImage:PlaceholderImage options:SDWebImageRefreshCached];
    // timeStampString 是服务器返回的13位时间戳
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval = [model.releaseDate doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [formatter stringFromDate: date];
    self.addressLabel.text = [ValidateUtils validateNull:model.origin];
    self.timeLabel.text = dateString;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    WS
//    if ([ValidateUtils validateNull:_model.typeImg].length > 0) {
//        self.mainImageView.hidden = false;
//        [self.mainTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(weakSelf.contentView.mas_left).with.offset(15);
//            make.top.equalTo(weakSelf.contentView.mas_top).with.offset(15);
//            make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-(120+20+20));
//        }];
//        [self.addressAndTimeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(weakSelf.contentView.mas_left).with.offset(15);
//            make.bottom.equalTo(weakSelf.contentView.mas_bottom).with.offset(-8);
//            make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-15);
//        }];
//    } else {
        self.mainImageView.hidden = true;
        [self.mainTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).with.offset(15);
            make.top.equalTo(weakSelf.contentView.mas_top).with.offset(15);
            make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-(20));
        }];
        [self.addressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).with.offset(15);
            make.bottom.equalTo(weakSelf.contentView.mas_bottom).with.offset(-8);
            make.width.mas_equalTo(ScreenWidth-15-15-80);
        }];
        [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.contentView.mas_bottom).with.offset(-8);
            make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-15);
        }];
//    }
}

- (UILabel *)mainTitleLabel {
    if (!_mainTitleLabel) {
        _mainTitleLabel = [[UILabel alloc] init];
        _mainTitleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:14];
        _mainTitleLabel.textColor = hexColor(333333);
        _mainTitleLabel.numberOfLines = 2;
    }
    return _mainTitleLabel;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] init];
        _addressLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:10];
        _addressLabel.textColor = hexColor(999999);
    }
    return _addressLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:10];
        _timeLabel.textColor = hexColor(999999);
    }
    return _timeLabel;
}

- (UIImageView *)mainImageView {
    if (!_mainImageView) {
        _mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth-120-20, 20, 120, 70)];
    }
    return _mainImageView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
